package com.hnevc.xiewenguang;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 *
 * 对于字符串排序程序设计
 * String和StringBuffer使用
 * 包装类的常用方法
 *
 */

public class Example03 {

    public static void main(String[] args) {
        String numStr = "20 78 9 -7 88 36 29";
        String[] strings = stringsToArray(numStr);
        int[] ints = toInArray(strings);
        String resultStr = arrayToString(ints);
        System.out.println(resultStr);
    }

    //将字符串以相同的规则进行转换为数组 split方法 分割字符串
    public static String[] stringsToArray(String numArr) {
        String[] strings = numArr.split(" ");
        return strings;
    }

    //通过包装类将字符串数组转换为整数类型数组
    public static int[] toInArray(String[] strings) {
        int[] ints = new int[strings.length];
        for (int i = 0; i < ints.length; i++) {
            //往长度为字符串数组的整数数组内添加元素
            ints[i] = Integer.parseInt(strings[i]);
        }
        Arrays.sort(ints); //升序排列
        return ints;
    }

    //将已经按照升序排列的整数数组装换为字符串
    public static String arrayToString(int[] ints) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < ints.length; i++) {
            if (i == 0) {
                stringBuffer.append(ints[i] + " ");
            }else {
                stringBuffer.append(" " + ints[i]);
            }
        }
        return stringBuffer.toString();
    }
}
